<template>
  <div class="content">
    <van-nav-bar title="推广审核" fixed @click-left="$router.back()" left-arrow>
    </van-nav-bar>
    <div class="poster_type">
      <ul>
        <li :class="{ active: type == 1 }" @click="setType(1)">
          文章
          <i></i>
        </li>
        <li :class="{ active: type == 2 }" @click="setType(2)">
          海报
          <i></i>
        </li>
        <li :class="{ active: type == 3 }" @click="setType(3)">
          视频
          <i></i>
        </li>
        <li :class="{ active: type == 4 }" @click="setType(4)">
          活动
          <i></i>
        </li>
        <li :class="{ active: type == 5 }" @click="setType(5)">
          招聘
          <i></i>
        </li>
      </ul>
    </div>

    <div>
      <div class="search">
        <van-search shape="round" placeholder="请搜索标题" />

        <div class="saixuan">
          <van-dropdown-menu active-color="#1989fa">
            <van-dropdown-item
              v-model="value1"
              :options="option1"
              @change="change"
            >
              <template #title>
                <img src="../../assets/images/index/saixuan.png" alt="" />
                筛选
              </template>
            </van-dropdown-item>
          </van-dropdown-menu>
          <!-- <img src="../../assets/images/index/saixuan.png" alt="" />
          筛选 -->
        </div>
      </div>

      <div class="list" v-if="checkArr.length != 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          :immediate-check="false"
          @load="onLoad"
        >
          <div
            class="item"
            v-for="(item, index) in checkArr"
            :key="index"
            @click.prevent="$router.push(`/${detail}?id=${item.id}`)"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.description }}</span>
              <div class="bottom">
                <div class="bleft">
                  <div class="check">
                    <img
                      v-if="item.audit == 2"
                      src="../../assets/images/video/checked.png"
                      alt=""
                    />
                    <img
                      v-else-if="item.audit == 1"
                      src="../../assets/images/video/checkno.png"
                      alt=""
                    />
                  </div>
                  <div class="uped">
                    <img
                      src="../../assets/images/video/up.png"
                      v-if="item.status == 2"
                      alt=""
                    />
                    <img
                      src="../../assets/images/video/noup.png"
                      alt=""
                      v-else
                    />
                  </div>
                </div>
                <van-icon name="ellipsis" @click.stop="clickAction(item)" />
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <van-empty description="暂无数据" v-else />
    </div>
    <!-- 弹框 -->
    <van-action-sheet
      v-model="showAction"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @cancel="showAction = false"
      @select="select"
    />
    <!-- 删除弹框 -->
    <van-dialog v-model="showDel" :show-confirm-button="false">
      <template #title>
        <van-icon name="cross" @click="showDel = false" />
      </template>
      <template #default>
        <div class="cont">
          <p>确定要删除该{{ types }}？</p>
          <van-divider />
          <div class="foot">
            <div class="left" @click="showDel = false">取消</div>
            <div class="right" @click="deleteArticlesEtc">确定</div>
          </div>
        </div>
      </template>
    </van-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      checkArr: [], //文章列表
      page: 1, //当前页数,
      totalPage: "",
      agentId: "",
      wxUserInfo: {},
      type: 1,
      tableType: 1,
      value1: 0,
      option1: [
        { text: "全部", value: 0 },
        { text: "已审核", value: 2 },
        { text: "未审核", value: 1 },
      ],
      showAction: false, //弹框
      audit: "", //审核状态
      showDel: false, //删除弹框
      itemObj: {}, //选择的项
      detail: "detail",
      loading: false, //加载状态
      finished: false, //完成
      page: 1, //页数
      limit: 8, //条数
      totalPage: "", //总页数
    };
  },
  computed: {
    actions() {
      return [
        { name: "审核" + this.types, id: 1 },
        {
          name:
            this.itemObj.status == 1
              ? "上架" + this.types
              : "下架" + this.types,
          id: 2,
        },
        { name: "预览" + this.types, id: 3 },
        { name: "删除" + this.types, id: 4 },
      ];
    },
    types() {
      if (this.type == 1) {
        return "文章";
      } else if (this.type == 2) {
        return "海报";
      } else if (this.type == 3) {
        return "视频";
      } else if (this.type == 4) {
        return "活动";
      } else {
        return "招聘";
      }
    },
  },
  created() {
    document.querySelector("html").style.backgroundColor = "#fff";
    document.querySelector("body").style.backgroundColor = "#fff";
  },
  async mounted() {
    this.wxUserInfo = JSON.parse(sessionStorage.getItem("userWxData"));
    this.setType(this.type);
  },

  methods: {
    //触底下一页
    onLoad() {
      if (this.page >= this.totalPage) {
        this.finished = true;
        return;
      }
      this.page++;

      this.setType(this.type);
      // 加载状态结束
      this.loading = false;
    },
    //选择审核待审核
    change(e) {
      this.audit = e;
      if (e == 0) {
        this.audit = "";
      }
      this.setType(this.type);
    },
    //点击触发选择弹框
    clickAction(item) {
      this.itemObj = item;
      this.showAction = true;
    },
    //选择
    select(e) {
      if (e.id == 4) {
        //删除
        this.showDel = true;
      } else if (e.id == 2) {
        // 上下架
        this.updateStatus(this.itemObj.status, this.itemObj.id);
      } else if (e.id == 3) {
        // 预览
        if (this.type == 1) {
          this.$router.push("/previewArticle?id=" + this.itemObj.id);
        } else if (this.type == 2) {
          this.$router.push("/previewPost?id=" + this.itemObj.id);
        } else if (this.type == 3) {
          this.$router.push("/previewVideo?id=" + this.itemObj.id);
        } else {
          this.$router.push("/releasepromotion?id=" + this.itemObj.id);
        }
      } else if (e.id == 1) {
        //审核
        if (this.type == 1) {
          this.$router.push(`/addAticle?id=${this.itemObj.id}&title=审核`);
        } else if (this.type == 2) {
          this.$router.push(`/addPoster?id=${this.itemObj.id}&title=审核`);
        } else if (this.type == 3) {
          this.$router.push(`/addVideo?id=${this.itemObj.id}&title=审核`);
        } else {
          this.$router.push(`/addRecruitment?id=${this.itemObj.id}&title=审核`);
        }
      }
    },
    //上下架
    async updateStatus(status, id) {
      if (status == 1) {
        status = 2;
      } else {
        status = 1;
      }
      let obj = {
        tableType: this.tableType,
        status,
        id,
        type: 2, //上下架 1审核2上下架
      };
      let res = await this.$api.updateStatus(obj);

      if (res.code != 0) return this.$toast(res.msg);
      this.setType(this.type);
    },
    //删除
    async deleteArticlesEtc() {
      let obj = {
        id: this.itemObj.id,
        tableType: this.tableType,
        userId: this.$store.state.userInfo.id,
      };
      let res = await this.$api.deleteArticlesEtc(obj);
      if (res.code != 0) return this.$toast(res.msg);
      this.showDel = false;
      this.setType(this.type);
    },
    // 切换
    setType(n) {
      this.checkArr = [];
      if (n == 1) {
        this.detail = "detail";
        this.tableType = 1;
        this.getArticleArr();
      } else if (n == 2) {
        this.detail = "postDetail";
        this.tableType = 3;
        this.getPosterList();
      } else if (n == 3) {
        this.detail = "videoDetail";
        this.tableType = 4;
        this.getVideoList();
      } else if (n == 5) {
        this.detail = "recruitmentdetails";
        this.tableType = 15;
        this.getRecruit();
      }
      this.type = n;
    },
    //获取文章任务列表
    async getArticleArr() {
      let obj = {
        account: this.$store.state.userInfo.account,
        page: this.page,
        limit: this.limit,
        openId: this.wxUserInfo.openid,
        name: this.wxUserInfo.nickname,
        img: this.wxUserInfo.headimgurl,
        tableType: 1,
        type: 9,

        audit: this.audit,
      };
      let res = await this.$api.getArticleArr(obj);
      if (res.code !== 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.checkArr = res.page.list || [];
      } else {
        this.checkArr.push(...res.page.list);
      }
      this.totalPage = res.page.totalPage;
    },
    //获取视频列表
    async getVideoList() {
      let res = await this.$api.getVideoList({
        account: this.$store.state.userInfo.account,
        page: this.page,
        limit: this.limit,
        agentId: this.$store.state.userInfo.account,
        tableType: 4,
        type: 9,
        audit: this.audit,
      });
      if (res.code != 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.checkArr = res.data.list || [];
      } else {
        this.checkArr.push(...res.data.list);
      }
      this.totalPage = res.data.totalPage;
    },
    //获取海报任务列表
    async getPosterList() {
      let res = await this.$api.getPosterList({
        account: this.$store.state.userInfo.account,
        page: this.page,
        limit: this.limit,
        agentId: this.$store.state.userInfo.account,
        tableType: 3,
        type: 9,
        audit: this.audit,
      });
      if (res.code != 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.checkArr = res.data.list || [];
      } else {
        this.checkArr.push(...res.data.list);
      }
      this.totalPage = res.data.totalPage;
    },
    //获取招聘列表
    async getRecruit() {
      let res = await this.$api.getRecruit({
        agentId: this.$store.state.userInfo.account,
        title: "",
        type: 9,
        audit: this.audit,
        page: this.page,
        limit: this.limit,
      });
      if (res.code != 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.checkArr = res.data.list || [];
      } else {
        this.checkArr.push(...res.data.list);
      }
      this.totalPage = res.data.totalPage;
    },
  },
};
</script>

<style scoped lang="less">
.poster_type {
  padding: 46px 30px 0;
  background-color: #fff;
  ul {
    display: -webkit-flex;
    display: flex;
    justify-content: space-between;
    li {
      font-size: 16px;
      color: #666;
      display: -webkit-flex;
      display: flex;
      justify-content: center;
      position: relative;
      align-items: center;
      padding: 5px 0;
      i {
        width: 28px;
        height: 3px;

        border-radius: 3px;
        position: absolute;
        bottom: 0;
      }
    }
    .active {
      font-weight: 700;
      color: #000;
      i {
        background-color: #0264d1;
      }
    }
  }
}
.content {
  padding: 0 15px;
  .search {
    display: flex;
    align-items: center;
    background-color: #fff;
    .van-search {
      flex: 1;
    }
    /deep/.van-dropdown-menu__title::after {
      display: none;
    }
    /deep/ .van-dropdown-menu__bar {
      background-color: rgba(10, 10, 11, 0);
      display: flex;
      align-items: center;
      box-shadow: 0 0 0;
    }
    /deep/.van-dropdown-menu__title {
      font-size: 11px;
      display: flex;
      align-items: center;
    }
    /deep/.van-ellipsis {
      overflow: visible;
      white-space: nowrap;
      text-overflow: none;
      display: flex;
      align-items: center;
    }
    .saixuan {
      display: flex;
      align-items: center;
      width: 50px;
      img {
        width: 14px;
        height: 14px;
        margin-right: 5px;
      }
      font-size: 11px;
      color: #666666;
    }
  }
  .list {
    height: calc(100vh - 185px);
    overflow-y: scroll;
    .item {
      display: flex;
      margin-top: 12px;
      .left {
        width: 90px;
        height: 90px;
        margin-right: 15px;
        position: relative;
        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
        .money {
          width: 35px;
          height: 35px;
          position: absolute;
          right: 0;
          top: 0;
          border-radius: none;
        }
      }
      .right {
        flex: 1;

        span {
          font-size: 11px;
          color: #999999;
        }
        .title {
          margin-top: 5px;
          display: block;
          font-size: 14px;
          color: #333333;
        }
        .p {
          margin-top: 3px;
          height: 30px;
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          text-overflow: ellipsis;
          -webkit-line-clamp: 2;
        }
        .bottom {
          width: 246px;
          margin-top: 8px;
          display: flex;
          justify-content: space-between;
          .bleft {
            display: flex;
            .check {
              img {
                width: 39px;
                height: 13px;
              }
            }
            .uped {
              margin-left: 11px;
              img {
                width: 29px;
                height: 12px;
                border-radius: 2px;
              }
            }
          }
        }
      }
    }
  }
}
/deep/ .van-dialog__header {
  text-align: right;
  padding: 15px 15px 0 0;
}
.van-dialog {
  width: 272px;
  .cont {
    font-size: 14px;
    font-weight: 700;
    color: #353535;
    text-align: center;
    p {
      padding: 20px 0;
    }
    .van-divider {
      margin: 0;
    }
    .foot {
      width: 100%;
      height: 43px;
      display: flex;
      .left,
      .right {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: #999999;
      }
      .right {
        color: #009dff;
        border-left: 1px solid #f5f6f8;
      }
    }
  }
}
</style>
